
import {router, addRoutes} from '~/router'
import { getToken } from '~/composables/auth'
import {toast, showFullLoading, hideFullLoading} from '~/composables/util'
import store from './store'

let hasGetInfo = false
// 全局前置
router.beforeEach( async (to, from, next) => {
    //显示loading
    showFullLoading()

    const token = getToken()
    if (!token && to.path != '/login') {
        toast("请登录", "warning")
        return next('/login')
    }
    
    // if (token && to.path == '/login') {
    //     toast("请勿重复登录", "warning")
    //     return next({path: from.path ? from.path : '/'})
    // }

    // let hasNewRoutes = false
    // // 如果用户登录了， 获取用户信息， 存储在vuex中
    // if (token && !hasGetInfo) {
    //     let {menus} = await store.dispatch("getInfo")
    //     hasGetInfo = true
    //     hasNewRoutes = addRoutes(menus)
    // }

    // 设置页面标题
    let title = (to.meta.title ? to.meta.title : "") + "-海兰信船舶服务"
    document.title = title
    // hasNewRoutes ? next(to.fullPath) : next()
    next()
})

// 全局后置钩子
router.afterEach((to, from) => {
    hideFullLoading()
})